//
//  ViewController.m
//  MultiTouch
//
//  Created by iD Student on 7/9/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    int rowNumber;
}

@synthesize recordPlay, goBack, goToBackgroundSwitcher, setBackgroundInstructionLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
  
        arrayOfColors = [[NSArray alloc]initWithObjects:self.view.backgroundColor,
                         [UIColor colorWithRed:0 green:161 blue:255 alpha:1],
                         [UIColor colorWithRed:0 green:255 blue:0 alpha:1],
                         [UIColor colorWithRed:170 green:0 blue:255 alpha:1],
                         [UIColor colorWithRed:255 green:84 blue:0 alpha:1],
                         [UIColor colorWithRed:255 green:0 blue:255 alpha:1],
                         [UIColor colorWithRed:0 green:67 blue:255 alpha:.5], nil];
    arrayofColorNames = [[NSArray alloc]initWithObjects:
                         [NSString stringWithFormat:@"Default Background Color"],
                         [NSString stringWithFormat:@"Baby Blue"],
                         [NSString stringWithFormat:@"Light Green"],
                         [NSString stringWithFormat:@"Purple"],
                         [NSString stringWithFormat:@"Yellow"],
                         [NSString stringWithFormat:@"Hot Pink"],
                         [NSString stringWithFormat:@"Dark Blue"],
                         nil];
    if(color != nil)
    {
        self.view.backgroundColor = color;
    } else {
        [touchRecordArray1 init];
        [touchRecordArray2 init];
        [touchRecordArray3 init];
        [touchRecordArray4 init];
    }
    if (self.view.tag == 1) {
   
        self.view.multipleTouchEnabled = TRUE;
        image = [[UIImage alloc]init];
        image = [UIImage imageNamed:@"ball.png"];
        image2 = [[UIImage alloc]init];
        image2 = [UIImage imageNamed:@"ball2.png"];
        timer = [NSTimer scheduledTimerWithTimeInterval:0.033 target:self selector:@selector(update) userInfo:nil repeats:YES];
        particles = [[NSMutableArray alloc]init];
        previousPoint = CGPointMake(160, 240);
        imageRocket = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"rocket.png"]];
    
        
    } else {
        
        pickerBackground.delegate = self;
        pickerBackground.dataSource = self;
        
    }
        
}
    
	// Do any additional setup after loading the view, typically from a nib.


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    touchesArray = [NSMutableArray arrayWithArray:[touches allObjects]];
    nNumTouches = [touchesArray count];
    for (int nTouch = 0;  nTouch < nNumTouches;  nTouch++)
    {
        touch = [touchesArray objectAtIndex:nTouch];
        ptTouch = [touch locationInView:self.view];
    }
    
    isTouching = true;
    touch = [touches anyObject];
    point = [touch locationInView:self.view];
    [NSTimer timerWithTimeInterval:.033 target:self selector:@selector(checkIfTouchMoved) userInfo:nil repeats:YES];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    isTouching = false;
    touchesArray = [NSMutableArray arrayWithArray:[touches allObjects]];
    nNumTouches = [touchesArray count];
    for (int i = 0; i < nNumTouches; i++) {;
        ptTouch = [[touchesArray objectAtIndex:i] locationInView:self.view];
        previousPoint = [[touchesArray objectAtIndex:i] previousLocationInView:self.view];
        int particlesToSpawn = 5;
        int maxLifeTime = 10;
        for (int i = 0; i < particlesToSpawn; i++) {
            Particle *particle = [[Particle alloc]init:image];
            particle.image.center = ptTouch;
            particle.velocityX = (previousPoint.x - ptTouch.x);
            particle.velocityY = (previousPoint.y - ptTouch.y);
            particle.lifetime = arc4random() % maxLifeTime + 30;
            [particles addObject:particle];
            [self.view addSubview:particle.image];
        }
        
    }
    
    
    
    
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   isTouching = false;
}

- (void) update
{
    NSLog([NSString stringWithFormat:@"%d",particles.count]);
    int i = 0;
    while (i < particles.count) {
        Particle *p = [particles objectAtIndex:i];
        if([p isAlive])
        {
                [p update:image];
            
            i++;
        } else {
            [p.image removeFromSuperview];
            [particles removeObject:p];
        }
    }
}

-(IBAction)recordPlay:(id)sender
{
    if([recordPlay.titleLabel.text isEqualToString:@"Record"])
    {
        recordPlay.titleLabel.text = @"Stop Recording";
        [self.view setUserInteractionEnabled:FALSE];
    }/* else if ([recordPlay.titleLabel.text isEqualToString:@"Stop Recording"]) {
        recordPlay.titleLabel.text = @"Play";
        [self.view setUserInteractionEnabled:true];
    } else if ([recordPlay.titleLabel.text isEqualToString:@"Play"]){
        recordPlay.titleLabel.text = @"Stop Playing";
        [self.view setUserInteractionEnabled:false];
    } else if ([recordPlay.titleLabel.text isEqualToString:@"Stop Playing"]) {
        recordPlay.titleLabel.text = @"Record";
    } else {
        [self.view setUserInteractionEnabled:true];
        
    }*/
}

- (void)insertData
{
    for (int i = 0; i < touchesArray.count; i++) {
        if(i == 0)
        {
            [touchRecordArray1 addObject:[touchesArray objectAtIndex:i]];
        } else if (i == 1) {
            [touchRecordArray2 addObject:[touchesArray objectAtIndex:i]];
        } else if (i == 2) {
            [touchRecordArray3 addObject:[touchesArray objectAtIndex:i]];
        } else if (i == 3) {
            [touchRecordArray4 addObject:[touchesArray objectAtIndex:i]];
        }
    }
}

- (void) playData
{
    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return arrayOfColors.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [arrayofColorNames objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    color = [arrayOfColors objectAtIndex:(row)];
    rowNumber = row + 1;
    self.view.backgroundColor = color;
    switch (rowNumber) {
        case 1:
            self.view.backgroundColor = color;
            recordPlay.titleLabel.textColor = [UIColor whiteColor];
            goToBackgroundSwitcher.titleLabel.textColor = [UIColor whiteColor];
            goBack.titleLabel.textColor = [UIColor whiteColor];
            setBackgroundInstructionLabel.textColor = [UIColor whiteColor];
            break;
        case 2:
            self.view.backgroundColor = color;
            recordPlay.titleLabel.textColor = [UIColor blackColor];
            goToBackgroundSwitcher.titleLabel.textColor = [UIColor blackColor];
            goBack.titleLabel.textColor = [UIColor blackColor];
            setBackgroundInstructionLabel.textColor = [UIColor blackColor];
            break;
        case 3:
            self.view.backgroundColor = color;
            recordPlay.titleLabel.textColor = [UIColor blackColor];
            goToBackgroundSwitcher.titleLabel.textColor = [UIColor blackColor];
            goBack.titleLabel.textColor = [UIColor blackColor];
            setBackgroundInstructionLabel.textColor = [UIColor blackColor];
            break;
        case 4:
            self.view.backgroundColor = color;
            recordPlay.titleLabel.textColor = [UIColor whiteColor];
            goToBackgroundSwitcher.titleLabel.textColor = [UIColor whiteColor];
            goBack.titleLabel.textColor = [UIColor whiteColor];
            setBackgroundInstructionLabel.textColor = [UIColor whiteColor];
            break;
        case 5:
           self.view.backgroundColor = color;
           recordPlay.titleLabel.textColor = [UIColor blackColor];
           goToBackgroundSwitcher.titleLabel.textColor = [UIColor blackColor];
            goBack.titleLabel.textColor = [UIColor blackColor];
            setBackgroundInstructionLabel.textColor = [UIColor blackColor];
            break;
        case 6:
           self.view.backgroundColor = color;
           recordPlay.titleLabel.textColor = [UIColor whiteColor];
           goToBackgroundSwitcher.titleLabel.textColor = [UIColor whiteColor];
            goBack.titleLabel.textColor = [UIColor whiteColor];
           setBackgroundInstructionLabel.textColor = [UIColor whiteColor];
            break;
        case 7:
            self.view.backgroundColor = color;
            recordPlay.titleLabel.textColor = [UIColor whiteColor];
            goToBackgroundSwitcher.titleLabel.textColor = [UIColor whiteColor];
            goBack.titleLabel.textColor = [UIColor whiteColor];
            setBackgroundInstructionLabel.textColor = [UIColor whiteColor];
            break;
        default:
            break;
    }
}


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    ViewController *controller =  segue.destinationViewController;
    if(color != nil)
    {
        switch (rowNumber) {
            case 1:
                controller.view.backgroundColor = color;
                controller.recordPlay.titleLabel.textColor = [UIColor whiteColor];
                controller.goToBackgroundSwitcher.titleLabel.textColor = [UIColor whiteColor];
                controller.goBack.titleLabel.textColor = [UIColor whiteColor];
                controller.setBackgroundInstructionLabel.textColor = [UIColor whiteColor];
            case 2:
                controller.view.backgroundColor = color;
                controller.recordPlay.titleLabel.textColor = [UIColor blackColor];
                controller.goToBackgroundSwitcher.titleLabel.textColor = [UIColor blackColor];
                controller.goBack.titleLabel.textColor = [UIColor blackColor];
                controller.setBackgroundInstructionLabel.textColor = [UIColor blackColor];
                break;
            case 3:
                controller.view.backgroundColor = color;
                controller.recordPlay.titleLabel.textColor = [UIColor blackColor];
                controller.goToBackgroundSwitcher.titleLabel.textColor = [UIColor blackColor];
                controller.goBack.titleLabel.textColor = [UIColor blackColor];
                controller.setBackgroundInstructionLabel.textColor = [UIColor blackColor];
                break;
            case 4:
                controller.view.backgroundColor = color;
                controller.recordPlay.titleLabel.textColor = [UIColor whiteColor];
                controller.goToBackgroundSwitcher.titleLabel.textColor = [UIColor whiteColor];
                controller.goBack.titleLabel.textColor = [UIColor whiteColor];
                controller.setBackgroundInstructionLabel.textColor = [UIColor whiteColor];
                break;
            case 5:
                controller.view.backgroundColor = color;
                controller.recordPlay.titleLabel.textColor = [UIColor blackColor];
                controller.goToBackgroundSwitcher.titleLabel.textColor = [UIColor blackColor];
                controller.goBack.titleLabel.textColor = [UIColor blackColor];
                controller.setBackgroundInstructionLabel.textColor = [UIColor blackColor];
                break;
            case 6:
                controller.view.backgroundColor = color;
                controller.recordPlay.titleLabel.textColor = [UIColor whiteColor];
                controller.goToBackgroundSwitcher.titleLabel.textColor = [UIColor whiteColor];
                controller.goBack.titleLabel.textColor = [UIColor whiteColor];
                controller.setBackgroundInstructionLabel.textColor = [UIColor whiteColor];
                break;
            case 7:
                controller.view.backgroundColor = color;
                controller.recordPlay.titleLabel.textColor = [UIColor whiteColor];
                controller.goToBackgroundSwitcher.titleLabel.textColor = [UIColor whiteColor];
                controller.goBack.titleLabel.textColor = [UIColor whiteColor];
                controller.setBackgroundInstructionLabel.textColor = [UIColor whiteColor];
                break;
            default:
                break;
        }
    }
}

@end
